package com.xia.ndk09_as_code;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public native String stringFromJNI();

    public native void testObject();

    public native void testDog();

    public native void releaseDog();

    //动态注册
    public native void registerJava01(String str);
    //测试线程
    public native void testThread();
    //释放线程
    public native void unThread();

    public void test01(View view) {
        testObject();
    }

    public void test02(View view) {
        testDog();
    }

    public void test03(View view) {
        releaseDog();
    }

    public void test04(View view) {
        registerJava01("李四AAAAAA");
    }

    public void test05(View view) {
        testThread();
    }

    //AndroidUi操作，让C++在线程中操作调用
    public void updateUI() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("UI")
                    .setMessage("更新UI")
                    .setPositiveButton("知道了", null)
                    .show();
        } else {
            Log.d(TAG, "updateUI: 子线程更新不了。。。。");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("UI")
                            .setMessage("更新UI")
                            .setPositiveButton("知道了", null)
                            .show();
                }
            });


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unThread();
    }
}
